package com.zhz.project.util;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;

import com.zhz.project.common.util.date.DateUtils;

public class getWebContent {

	public static final String proxy = "222.77.14.55";
	public static final int port = 8088;

	// public static final String WEB_ROOT = System.getProperty("user.dir")
	// + File.separator + "binaryresources";

	public static final String WEB_ROOT = "E:\\binaryresources";

	public static String getContent(String myurl) {
		return getCustomedStringContent(myurl, null);
	}

	/**
	 * 获取web资源， 如果返回的是html文件，则返回文件内容 如果返回的是二进制文件，则保存该文件，然后
	 * 
	 * @param strUrl
	 * @param cookie
	 * @param binary
	 * @return
	 */
	public static String getSocolarContentHttpURLConnection(String strUrl,
			String cookie, boolean binary) {

		Map<String, String> map = new HashMap<String, String>();
		map.put("Cookie", "ASP.NET_SessionId=" + cookie);

		map.put("User-Agent",
				"Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13");
		if (!binary) {
			return getCustomedStringContent(strUrl, map);
		} else {
			return getCustomedBinaryContent(strUrl, map);
		}
	}

	public static String getCustomedStringContent(String strUrl,
			Map<String, String> map) {
		return getCustomedContent(strUrl, map, false);
	}

	public static String getCustomedBinaryContent(String strUrl,
			Map<String, String> map) {
		return getCustomedContent(strUrl, map, true);
	}

	private static String getCustomedContent(String strUrl,
			Map<String, String> map, boolean binary) {

		try {
			HttpClient client = new HttpClient();
			client.getHostConfiguration().setProxy(getWebContent.proxy,
					getWebContent.port);
			HttpMethod method = new GetMethod(strUrl);
			method.setRequestHeader("Connection", "close");
			List<Header> headers = new ArrayList<Header>();

			if (map != null && map.size() > 0) {
				Set<Entry<String, String>> set = map.entrySet();
				for (Entry<String, String> obj : set) {
					String key = obj.getKey();
					String value = obj.getValue();
					headers.add(new Header(key, value));

				}
			}

			client.getHostConfiguration().getParams()
					.setParameter("http.default-headers", headers);

			client.executeMethod(method);

			if (!binary) {
				return method.getResponseBodyAsString();
			} else {
				byte[] b = method.getResponseBody();
				String fileName = DateUtils.dtLongMillFormat(new Date());
				FileOutputStream os = new FileOutputStream(WEB_ROOT + "\\"
						+ fileName + ".gif");
				os.write(b);
				return WEB_ROOT + "\\" + fileName + ".gif";
			}
		} catch (Exception e) {
			return "error open url" + strUrl;
		}
	}

}
